﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UI
{
    public class TestBattlePage : Page
    {
        public TestBattleSelectPage select;
        public List<TestBattleFormation> formations;
        protected override void DoOpen()
        {
        }

        public List<TestBattleFormation> GetFormation(int camp)
        {
            List<TestBattleFormation> fs = new List<TestBattleFormation>();
            foreach (var f in formations)
            {
                if (f.camp == camp)
                    fs.Add(f);
            }
            return fs;
        }

        public TestBattleFormation GetFormation(int camp, int pos)
        {
            foreach (var f in formations)
            {
                if (f.camp == camp && f.pos == pos)
                {
                    return f;
                }
            }
            return null;
        }

        public void OnItemSelect(string heroId, int camp, int pos)
        {
            var f = GetFormation(camp, pos);
            f.Refresh(heroId);
        }

        public void ShowSelectPanel(int camp, int pos)
        {
            select.Open(camp, pos);
        }

        public void HideSelectPanel()
        {
            select.Close();
        }

        public void Fight()
        {
            StaticData.isTestBattle = true;
            LoadingPanel.Instance.ShowLoading();
            CameraTool.Instance.SwitchBattleCamera();
            PageManager.Instance.HideUI();

            StaticData.curBattleData.SetTestData1(GetAttackerHeroIds(), GetDefenderHeroIds());

            Room.RoomManager.Switch(SceneType.BATTLE);
            Battle.BattleManager.Initialized();
            Battle.BattleManager.Instance.NewFight();
        }

        List<string> GetAttackerHeroIds()
        {
            List<string> res = new List<string>();
            foreach (var f in GetFormation(0))
            {
                if (!string.IsNullOrEmpty(f.heroId))
                {
                    res.Add(f.heroId);
                }
            }
            return res;
        }

        List<string> GetDefenderHeroIds()
        {
            List<string> res = new List<string>();
            foreach (var f in GetFormation(1))
            {
                if (!string.IsNullOrEmpty(f.heroId))
                {
                    res.Add(f.heroId);
                }
            }
            return res;
        }
    }
}